﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace MainGame.GameForm
{
    public class VMenu : FormControl
    {
        public VMenu(ContentManager content)
            : base(content)
        {
            this.Margin = 2;
        }

        public override void ValidateUI()
        {
            base.ValidateUI();
            Vector2 size = this.Size;
            float menuWidth = size.X;
            float menuHeight = size.Y;
            var pos = this.Position;
            for (int i = 0; i < this.Items.Count; i++)
            {
                var button = this.Items[i];
                float dx = menuWidth > button.Size.X ? (menuWidth - button.Size.X) / 2 : 0;
                pos.X = this.Position.X + dx;
                button.Position = pos;
                pos.Y += button.Size.Y + Margin;
            }
            var first = this.Items.First();
            var last = this.Items.Last();

            float totalHeight = last.Position.Y + last.Size.Y - first.Position.Y;
            if (size.Y > totalHeight)
            {
                float dy = (totalHeight - size.Y) / 2;
                Items.ForEach(i => i.Position -= new Vector2(0, dy));
            }
        }
        
    }
}
